﻿using System.Collections.Generic;
using System.Text;

namespace Borg.Framework
{
    public class FindCommand : ICommand
    {
        private readonly string[] _arguments =
            new[]
                {
                    "PluginInfo",
                    "ExtensionTypeInfo",
                    "PlugTypeInfo",
                    "SlotTypeInfo",
                    "ExtensionInfo",
                    "Plug",
                    "SlotInfo",
                    "ContractInfo",
                    "SlotDefinition"
                };

        private string Find(string[] command)
        {
            string cmd;

            if (command.Length < 2)
                return "Too few arguments.";

            if (!RuntimeCommands.ContainsArgument(command[1], _arguments))
                return string.Format("Invalid argument \"{0}\".", command[1]);

            if (command.Length < 3)
                return "Too few arguments.";

            var elements = RuntimeCommands.Find(command, 1, out cmd);

            if (elements.Length == 0)
                return cmd;

            var builder = new StringBuilder();

            foreach (var element in elements)
                builder.AppendFormat("{0}{1}", element, RuntimeCommands.lf);

            return builder.ToString();
        }

        public IEnumerable<string> GetArguments(string[] command, int pos)
        {
            return pos != 0
                ? null
                : _arguments;
        }

        public CommandHandler Handler
        {
            get { return Find; }
        }

        public string Help
        {
            get { return "help for command \"find\""; }
        }

        public string Name
        {
            get { return "find"; }
        }
    }
}

